/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kaytto1;

import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.MediaComponent;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BoxLayout;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;

/**
 *
 * @author Joteitti
 */
public class CameraForm extends Form {

    public static final String DEFAULT_CAMERA_PLAYER = "capture://video";
    public static final int CAMERA_HEIGHT_FIX = -2;

    private HomeService homeService;
    private Player cameraPlayer;
    private MediaComponent cameraComponent;

    public CameraForm(HomeService homeService) {
        super();

        // Set home service
        this.homeService = homeService;

        // Create form layout and components
        setTitle(Text.getString(Text.TEXT_CAMERA_TITLE));
        addCommandListener(homeService);
        addCommand(homeService.getTakePictureCommand());
        addCommand(homeService.getContinueCommand());
        addCommand(homeService.getBackCommand());
        setTransitionOutAnimator(homeService.getTransition());
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        addComponent(getCameraComponent());
    }

    public byte[] takePicture() {
        // Take picture
        try {
            VideoControl videoControl = (VideoControl) cameraComponent.getVideoControl();
            return videoControl.getSnapshot(null);
        } catch (Exception e) {
            homeService.showExitDialog(e.getMessage(), e.toString());
            return null;
        }
    }

    private Player getCameraPlayer() {
        // Lazy initialization
        if(cameraPlayer == null) {
            try {
                cameraPlayer = Manager.createPlayer(DEFAULT_CAMERA_PLAYER);
                cameraPlayer.prefetch();
                cameraPlayer.realize();
            } catch(Exception e) {
                homeService.showExitDialog(e.getMessage(), e.toString());
            }
        }

        return cameraPlayer;
    }

    private MediaComponent getCameraComponent() {
        // Lazy initialization
        if(cameraComponent == null) {
            cameraComponent = new MediaComponent(getCameraPlayer());
            int menuHeight = getSoftButton(0).getParent().getPreferredH();
            int titleHeight = getTitleComponent().getPreferredH();
            int width = Display.getInstance().getDisplayWidth();
            int height = Display.getInstance().getDisplayHeight() - (menuHeight + titleHeight) + CAMERA_HEIGHT_FIX;
            cameraComponent.setWidth(width);
            cameraComponent.setHeight(height);
            cameraComponent.setPreferredSize(new Dimension(width, height));
        }

        return cameraComponent;
    }

    protected void focusGained() {
        super.focusGained();

        // Show camera
        cameraComponent.setVisible(true);
        cameraComponent.start();
    }

    protected void focusLost() {
        super.focusLost();

        // Hide camera
        cameraComponent.setVisible(false);
        cameraComponent.stop();
    }

    public void setVisible(boolean visible) {
        super.setVisible(visible);

        if(cameraComponent != null) {
            if(visible) {
                // Show camera
                cameraComponent.setVisible(true);
                cameraComponent.start();
            } else {
                // Hide camera
                cameraComponent.setVisible(false);
                cameraComponent.stop();
            }
        }
    }
    
}
